﻿using System;
using System.Net;
using System.Net.Mail;

namespace AutoTask
{
	public class WeeklyEmail:Singleton<WeeklyEmail>
	{
		/// <summary>
		/// 发送邮件
		/// </summary>
		/// <param name="from">发送者邮箱</param>
		/// <param name="fromer">发送人</param>
		/// <param name="to">接收者邮箱</param>
		/// <param name="toer">收件人</param>
		/// <param name="subject">主题</param>
		/// <param name="body">内容</param>
		/// <param name="file">附件</param>
		/// <param name="smtpHost">smtp服务器</param>
		/// <param name="smtPuser">邮箱</param>
		/// <param name="smtPpass">密码</param>
		/// <returns></returns>
		public bool SendWeeklyMail(string from, string fromer, string to, string toer, string subject, string body, string file, string smtpHost, string smtPuser, string smtPpass)
		{
			//设置from和to地址
			MailAddress fromAddress = new MailAddress(from, fromer);
			MailAddress toAddress = new MailAddress(to, toer);

			//创建一个MailMessage对象
			MailMessage mailMessage = new MailMessage(fromAddress, toAddress);

			// 添加附件
			if (!string.IsNullOrEmpty(file))
				mailMessage.Attachments.Add(new Attachment(file));
			//邮件标题
			mailMessage.Subject = subject;
			//邮件内容
			mailMessage.Body = body;
			//邮件格式
			mailMessage.IsBodyHtml = false;
			//邮件采用的编码
			mailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
			//设置邮件的优先级为高
			mailMessage.Priority = MailPriority.High;

			////发送邮件
			SmtpClient client = new SmtpClient();
			////client.UseDefaultCredentials = false; 
			client.Host = smtpHost;
			client.Credentials = new NetworkCredential(smtPuser, smtPpass);
			client.DeliveryMethod = SmtpDeliveryMethod.Network;
			try
			{
				client.Send(mailMessage);
				return true;
			}
			catch (Exception err)
			{
				Console.WriteLine(err.Message.ToString());
				return false;
			}
			finally
			{
				////释放资源
				mailMessage.Dispose();
			}
		}


	}
}